<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" >
      <el-form-item label="日期" prop="holidayDate">
        <el-input
          v-model="queryParams.holidayDate"
          placeholder="请输入日期"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="节日" prop="holidayName">
        <el-input
          v-model="queryParams.holidayName"
          placeholder="请输入节日"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['labdata:holiday:edit']"
          >新增</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="holidayList" >
      <el-table-column label="编号" align="center" prop="dayId" />
      <el-table-column label="日期" align="center" prop="holidayDate" />
      <el-table-column label="节日" align="center" prop="holidayName" />
      <el-table-column label="工作日" align="center" prop="holidayFlag" :formatter="flagOptFormat"/>
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="时间" align="center" prop="createTime" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" v-if="isPermit('labdata:holiday:edit')">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改工作日对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="form.dayId?rules1:rules" label-width="80px">
        <el-form-item label="日历年份" prop="year" v-if="form.dayId==null">
          <el-date-picker clearable size="small" style="max-width: 200px;" v-model="form.year" type="year"
            value-format="yyyy" placeholder="选择生成日历的年份" class="input_w">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="日期" prop="holidayDate" v-if="form.dayId">
          <el-input v-model="form.holidayDate" placeholder="请输入日期" disabled class="input_w"/>
        </el-form-item>
        <el-form-item label="名称" prop="holidayName" v-if="form.dayId">
          <el-input v-model="form.holidayName" placeholder="请输入名称" class="input_w"/>
        </el-form-item>
        <el-form-item label="工作日" prop="holidayFlag" v-if="form.dayId">
          <el-select v-model="form.holidayFlag" placeholder="请选择标识" class="input_w">
            <el-option v-for="dict in holidayFlagOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark" v-if="form.dayId">
          <el-input v-model="form.remark" placeholder="请输入备注" class="input_w"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listHoliday, getHoliday, delHoliday, addHoliday, updateHoliday, exportHoliday } from "@/api/labdata/holiday";

export default {
  name: "Holiday",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工作日表格数据
      holidayList: [],
      holidayFlagOptions:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        holidayDate: null,
        holidayName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        year: [
          { required: true, message: "年份不能为空", trigger: "blur" }
        ],
      },
      rules1: {
        holidayName: [
          { required: true, message: "节日不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() { //
    this.getList();
    this.getDicts("isOrNotOpt").then(response => {
      this.holidayFlagOptions = response.data;
    });
  },
  methods: {
    // 单位字典翻译
    flagOptFormat(row, column) {
      return this.selectDictLabel(this.holidayFlagOptions, row.holidayFlag);
    },
    /** 查询工作日列表 */
    getList() {
      this.loading = true;
      listHoliday(this.queryParams).then(response => {
        this.holidayList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.dayId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加工作日";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const dayId = row.dayId || this.ids
      getHoliday(dayId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改工作日";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.dayId != null) {
            updateHoliday(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addHoliday(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const dayIds = row.dayId || this.ids;
      this.$confirm('是否确认删除工作日编号为"' + dayIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delHoliday(dayIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    isPermit(permitKey){
      if(this.$store.state.user.name=='admin'){
        return true;
      }
      let  permissions = this.$store.getters.permissions;
      return permissions.includes(permitKey);
    },
  }
};
</script>
<style type="text/css" scoped>
  .input_w{
    width: 300px;
  }
</style>
